class GwtSellerController<GwtController
  @@LOG_FLAG="GwtSellerController"
  def agent
    @agents = WWW::Tkd::Agent.find(:all)
    render :text => @agents.to_json
  end
  def index
    user_id = session[:id]
    start = params[:start]
    limit = params[:limit]
    if user_id==nil
      render :text => '请通过阿里平台登录后'
      return
    end
    orders = WWW::Tkd::TbSellerOrder.find_on_page(user_id,start.to_i,limit.to_i)
    @tb_orders=Array.new
    orders.each do|item|
      @tb_orders.push(WWW::Tkd::GwtSellerOrder.init_by_order(item))
    end
    result = Hash.new
    result['offset']=start.to_i
    result['count']=WWW::Tkd::TbSellerOrder.count user_id
    result['items']=@tb_orders
    render :text => result.to_json
  end
  def view
      #预先判定是否是淘宝用户
      domain_login_id=params['domain_login_id']
      if domain_login_id!=nil
            conv = Iconv.new("UTF-8","GB2312")
            domain_login_id = conv.iconv(domain_login_id)
            user_id = params['user_id']
            app_instance_id = params['app_instance_id']
            hash_code='xxxx'

            tb_user=WWW::Tkd::TbUser.save_from_aliww(domain_login_id,user_id,app_instance_id,hash_code,1)
            if tb_user==nil
               render :text =>'尊敬的用户，本次登录为非法'
               return
            end

            session[:user_id]=tb_user.user_id
            session[:id]=tb_user.id
            session[:sip_sessionid]=tb_user.sip_sessionid
            session[:domain_login_id]=tb_user.domain_login_id

            app_id='17886'
            cert_code='f14a71f0b09011ddb3e6cfd602199686'
            sip_url_pre='http://sip.alisoft.com/sip/rest'
            isv_url_pre='http://seller.taokuaidi.cn/gwt_seller/success'
            WWW::Ali.set_app_id app_id
            WWW::Ali.set_cert_code cert_code
            WWW::Ali.set_sip_url_pre sip_url_pre
            WWW::Ali.set_isv_url_pre isv_url_pre
    end
    if session[:id]==nil
      render :text => '请通过阿里平台登录后进行下一步操作'
      WWW::Tkd::Logger.info  "#{@@LOG_FLAG}indexnolegal",""
      return
    end
    #获取数据并验证是否登录
     @isAPIExtendTime = is_valid_login
    #管理员入口
     if params[:user_login_name]!=nil
        conv = Iconv.new("UTF-8","GB2312")
        user_login_name = conv.iconv(params[:user_login_name])
        tb_user=WWW::Tkd::TbUser.find_by_domain_login_id (user_login_name)
        if tb_user==nil
           render :text => '用户 ['+user_login_name+'] 不存在，请检查'
           return
        end
        session[:id]=tb_user.id
     end

    @user=WWW::Tkd::TbUser.find session[:id]
  end
  #卖家手工输入订单信息
  def saveno
    user_id = session[:id]
    if user_id==nil
      result[:flag]=-1
      result[:msg]="请登录后操作"
      render :text => result.to_json
    end
    result = Hash.new
    agent_id = params[:agent_id]
    no_code=params[:no_code]
    if no_code==nil||no_code.length<6
      result[:flag]=-1
      result[:msg]="快递号请检查，谢谢啦"
      WWW::Tkd::Logger.info  "#{@@LOG_FLAG}sellerexpressnotlength",""
      render :text => result.to_json
    end
    order_id=params[:order_id].to_i
    agent = WWW::Tkd::Agent.find_by_id agent_id
    if agent==nil
      result[:flag]=-1
      result[:msg]="快递公司不存在"
      render :text => result.to_json
      return
    end
    #组装TbOrder对象
    seller_order = WWW::Tkd::TbSellerOrder.find_from_order_id_and_user_id(order_id,user_id)
    if(seller_order==nil)
      result[:flag]=-2
      result[:msg]="交易订单非本用户"
      render :text => result.to_json
      return
    end
    #组装Express对象
    express=WWW::Tkd::Express.new
    express.agent_id=agent_id
    express.no_code=no_code.strip()
    #分解对象并进行保存
    order = WWW::Tkd::TbOrder.find(order_id)
    if order==nil
      result[:flag]=-3
      result[:msg]="交易订单不存在"
      render :text => result.to_json
      return
    end
    WWW::Tkd::GwtSellerOrder.save seller_order,express,user_id
    result[:flag]=1
    result[:msg]="success"
    render :text => result.to_json
  end
end
